<?php

namespace App\Kuafu\CouponExt\Channel\MeiTuan;

use App\Kuafu\CouponExt\Enum\CouponExtEnum;
use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Pay\Channel\FuYou\FuYouPayMock;
use Illuminate\Support\Facades\Cache;

class MeiTuanBaseMock extends MeiTuanBase
{
    const CONSUME_KEY = 'CONSUME_KEY:';

    public function __construct(int $shopId, bool $configLoad = true, bool $fsWarning = true)
    {
        parent::__construct($shopId, $configLoad, $fsWarning);
    }

    /**
     * 验券准备
     * @param string $couponCode
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function couponPrepare(string $couponCode): array
    {
        self::mockUsleep();

        $format         = self::returnFormatData(true, '', '', []);
        $format['data'] = [
            'count'           => 100000,
            // 关联张数，最大可验证张数
            'couponSid'       => 1,
            // 券的关联id
            'couponTitle'     => '自己测试的牛逼券',
            // 券名称
            'couponCode'      => $couponCode,
            // 券码
            'couponType'      => CouponExtEnum::COUPON_TYPE_MONEY,
            // 券类型
            'userPay'         => 300,
            // 用户支付的钱
            'deduction'       => 2400,
            // 可抵扣的钱
            'couponBeginTime' => 0,
            // 券可开始使用时间
            'couponEndTime'   => strtotime(20331231),
            // 券使用截止日期
            'minConsume'      => 1,
            // 最小消费张数
            'dealMenu'        => [],
        ];
        return $format;
    }

    /**
     * 核销券
     * @param string $couponCode
     * @param int $count
     * @param string $orderId
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function couponConsume($couponCode, int $count, string $orderId): array
    {
        self::mockUsleep();

        $format      = self::returnFormatData(true, '', '', []);
        $couponCodes = [];
        for ($i = 0; $i < $count; $i++) {
            if ($count == 1) {
                $couponCodes[] = ['couponCode' => $couponCode];
            } else {
                $couponCodes[] = ['couponCode' => BaseService::createIncrNo(14)];
            }
        }

        $format['data'] = [
            'couponCodes' => $couponCodes,
            'couponSid'   => 1,
            'couponTitle' => '自己测试的牛逼券',
            'deduction'   => 10 * $count,
        ];

        $redisKey = sprintf(self::CONSUME_KEY . '%s', $couponCode);
        Cache::store('redis')->tags(FuYouPayMock::CACHE_TAG)->put($redisKey, 1, 1200);

        return $format;
    }

    /**
     * 撤销验券
     * @param string $couponCode
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function couponCancel(array $couponInfo): array
    {
        $couponCode = $couponInfo['couponExtCode'];
        self::mockUsleep();

        $redisKey = sprintf(self::CONSUME_KEY . '%s', $couponCode);
        Cache::store('redis')->tags(FuYouPayMock::CACHE_TAG)->forget($redisKey);
        return [
            'status' => true
        ];
    }

    /**
     * 已验券码查询
     * @param string $couponCode
     * @return array
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function couponQuery(string $couponCode): array
    {
        self::mockUsleep();

        $redisKey = sprintf(self::CONSUME_KEY . '%s', $couponCode);
        if (!Cache::store('redis')->tags(FuYouPayMock::CACHE_TAG)->has($redisKey)) {
            return ['status' => false];
        }

        $format['data'] = [
            'userPay'          => 1,
            'deduction'        => 10,
            'cancelStatus'     => 1, // 0不可撤销 1可撤销
            'couponStatusDesc' => '', // 使用状态文字
            'couponUseTime'    => 0, // 券使用时间
            'couponBeginTime'  => 0, // 项目开始时间
            'couponSid'        => 1,
            'couponTitle'      => '',
            'couponCode'       => $couponCode,
            'couponType'       => CouponExtEnum::COUPON_TYPE_MONEY,
        ];

        return $format;
    }

    public static function mockUsleep(int $num = 200)
    {
        usleep($num * 1000);
    }
}
